'''
Created on Aug 2, 2012

@author: loic
'''

import os, sys, csv, shutil
from subprocess import Popen,PIPE

dir = os.getcwd()
filename = '/home/loic/mp3list.csv'
dest = '/media/PUBLIC'

def readdir():
    global dir
    
    writer = csv.writer(open(filename, 'ab'), dialect=csv.excel_tab)
    one = os.listdir('.')
    try:
        for o in one:
    #        print o
            if not os.path.isdir('./'+o):
                continue
    #        print o
            two = os.listdir('./'+o)
            for t in two:
                f = '%s/%s/%s' % (dir, o, t)
    #            print '-->'+f
    #            s1 = Popen('du -sk \'%s\'' % f, stdout=PIPE, shell=True).stdout.read()
    #            s2 = s1.split()
    #            s3 = s2[0]
    ##            print s
    #            print '-->'+s1
    #            print s2
    #            print '---->'+s3
                writer.writerow([f, '%s_%s' % (o, t)])
                print '%s\t%s_%s' % (f, o, t)
    except:
        pass


def copyPL():
    global dest
    reader=csv.reader(open('/home/loic/playlist.csv', 'r'), dialect=csv.excel_tab)
    i = 1
    for row in reader:
        print 'copying %s' % row[0],
        name = '%s/%03d_%s' % (dest, i, row[1])
        i += 1
        print ' to %s' % name
        try:
            shutil.copytree(row[0], name)
        except:
            pass
    
if __name__ == '__main__':
    copyPL()
